/**
 * 
 */
package com.cmwa.solution.sys.menu.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.cmwa.solution.utils.cached.DictionaryCached;

/**
 * 数据字典转换
 * @author ex-zuotc
 *
 * 2016年8月18日
 */
public class DictConvertorTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	/** 字典值 */
	protected String dctValue;
	/** 字典类型 */
	protected String dctRootType;
	protected String dctFathType;
	protected String dctLeftType;
	/** 是否为复选值 */
	protected boolean multi = false;
	/** 复选时分隔符 */
	protected String multiSeparator = ",";
	/**
	 * 显示时分隔符
	 */
	protected String displaySeparator = ",";

	public int doStartTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			String resultStr = null;
			if (dctValue != null && !dctValue.isEmpty()) {
				if (!multi) {
					resultStr = DictionaryCached.getDictName(dctRootType, dctValue);
					if (dctFathType != null && dctLeftType != null) {
						resultStr = DictionaryCached.getDictName(dctRootType, dctFathType, dctLeftType, dctValue);
					}
				} else {
					resultStr = DictionaryCached.getDictNameByArry(dctRootType, dctValue, multiSeparator);
					if (displaySeparator != null) {
						if (StringUtils.equals(displaySeparator, "br")) {
							displaySeparator = "<br>";
						}
						
						String[] resultArray = resultStr.split(multiSeparator);
						if (resultArray != null && resultArray.length > 0) {
							StringBuffer stringBuffer = new StringBuffer();
							for (int i = 0; i < resultArray.length; i++) {
								stringBuffer.append(resultArray[i]);
								if (i != resultArray.length - 1) {
									stringBuffer.append(displaySeparator);
								}
							}
							resultStr = stringBuffer.toString();
						}
					}
				}
			}
			
			if (resultStr != null) {
				out.print(resultStr);
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * @return the dctValue
	 */
	public String getDctValue() {
		return dctValue;
	}

	/**
	 * @param dctValue the dctValue to set
	 */
	public void setDctValue(String dctValue) {
		this.dctValue = dctValue;
	}

	/**
	 * @return the dctRootType
	 */
	public String getDctRootType() {
		return dctRootType;
	}

	/**
	 * @param dctRootType the dctRootType to set
	 */
	public void setDctRootType(String dctRootType) {
		this.dctRootType = dctRootType;
	}

	/**
	 * @return the multi
	 */
	public boolean isMulti() {
		return multi;
	}

	/**
	 * @param multi the multi to set
	 */
	public void setMulti(boolean multi) {
		this.multi = multi;
	}

	/**
	 * @return the multiSeparator
	 */
	public String getMultiSeparator() {
		return multiSeparator;
	}

	/**
	 * @param multiSeparator the multiSeparator to set
	 */
	public void setMultiSeparator(String multiSeparator) {
		this.multiSeparator = multiSeparator;
	}

	/**
	 * @return the displaySeparator
	 */
	public String getDisplaySeparator() {
		return displaySeparator;
	}

	/**
	 * @param displaySeparator the displaySeparator to set
	 */
	public void setDisplaySeparator(String displaySeparator) {
		this.displaySeparator = displaySeparator;
	}

	public String getDctFathType() {
		return dctFathType;
	}

	public void setDctFathType(String dctFathType) {
		this.dctFathType = dctFathType;
	}

	public String getDctLeftType() {
		return dctLeftType;
	}

	public void setDctLeftType(String dctLeftType) {
		this.dctLeftType = dctLeftType;
	}
	
}
